﻿
package com.sci;

import java.util.ArrayList;


public class Main
{
	public static String outPath = System.getProperty("user.dir");
	
	/** @param args */
	public static void main(String[] args)
	{
		// args = new String[] { "D:\\sci\\小软件\\batchSign\\apks\\Empty.apk" };
		String[] files = Tools.getSubFiles(args);	// 获取args对应的所有目录下的文件列表
		for (String apk : files)
		{
			getSignInfo(apk);
		}
	}
	
	/** 获取apk的签名信息 */
	public static void getSignInfo(String apkPath)
	{
		if (apkPath.toLowerCase().endsWith(".apk"))
		{
			System.out.println("-");
			System.out.println("获取apk签名信息，文件：" + apkPath);
			String RSA = get_METAINF_RSA(apkPath);			// 获取apk包中的*.RSA签名文件
			if (!RSA.equals(""))
			{
				ShowKeyToolInfo(RSA);						// 获取签名文件信息
				Tools.Delete(outPath + "/META-INF");		// 删除解压出的签名文件
			}
			else System.out.println("apk中无签名信息！");
		}
		else System.out.println("仅支持查看apk中的签名信息！");
	}
	
	// res/drawable/empty_ic_launcher.png
	// res/layout/empty_activity_main.xml
	// AndroidManifest.xml
	// resources.arsc
	// classes.dex
	// META-INF/MANIFEST.MF
	// META-INF/CERT.SF
	// META-INF/CERT.RSA
	/** 获取Apk包的META-INF文件夹 */
	public static String get_METAINF_RSA(String zipPath)
	{
		ArrayList<String> list = Tools.getZipSubFile(zipPath, outPath, "META-INF/.*.RSA");	// 解压RSA文件
		// File fileRsa = Tools.getFileEndsWith(outPath + "/META-INF", ".RSA");
		return list.isEmpty() ? "" : list.get(0);											// 返回RSA文件路径
	}
	
	/** 输出显示apk签名信息 */
	public static boolean ShowKeyToolInfo(String RsaPath)
	{
		String[] cmd = { "keytool", "-printcert", "-file", RsaPath };
		try
		{
			Cmd.execute(cmd, "GBK", new Cmd.Callback()
			{
				@Override
				public void onCallback(String s)
				{
					String[] startsWith = { "所有者:", "发布者:", "有效期", "证书指纹:", "\t MD5:", "\t SHA1:", "\t SHA256:" };
					for (String start : startsWith)
					{
						if (s.startsWith(start))
						{
							System.out.println(s);
						}
					}
				}
			});
		}
		catch (Exception e)
		{
			System.out.println("读取游戏签名失败");
			e.printStackTrace();
		}
		return true;
	}
	
}
